const { ccclass, property } = cc._decorator;
declare let i18n,DBManager,dbh;
import BaseView from "../../component/BaseView";
import UIHelper = require("../../component/UIHelper");
import PopUIHelper = require("../../utils/PopUIHelper");
import Utils = require("../../utils/Utils");
//糟糕的化妆  关卡10
@ccclass
export default class DeputyMakeUpView extends BaseView {
    @property(cc.Label)
    lblStep: cc.Label = null;
    @property(cc.Label)
    lblDes: cc.Label = null;

    @property(sp.Skeleton)
    spine:sp.Skeleton = null;

    @property(cc.Node)
    shuziNode: cc.Node = null;
    @property(cc.Node)
    kouhongNode: cc.Node = null;

    @property(cc.Node)
    pengNode: cc.Node = null;

    @property(cc.Node)
    wrongNode: cc.Node = null;

    //恭喜获得
    @property(cc.Node)
    endNode: cc.Node = null;

    _done = false;
    _selectPos1 = cc.v2(-100,-520);
    _selectPos2 = cc.v2(100,-520);
    _isStart = false;
    onLoad() {
        Utils.audioManager.playBGM("huazhuang/deputyGame/deputyGameBg");
        this.shuziNode.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.shuziNode.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
        this.shuziNode.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.kouhongNode.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove1, this);
        this.kouhongNode.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd1, this);
        this.kouhongNode.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd1, this);
        //@ts-ignore
        this._isStart = this.node.openParam;
        this.lblStep.string = i18n.t("DEPUTY_GAME_STEP_TITLE10");
        this.lblDes.string = i18n.t("DEPUTY_GAME_STEP_DES10");
    };
    onClose(){
        Utils.audioManager.playDefaultBGM();
    };
    onTouchMove(event){
        if(this._done)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.shuziNode.parent.convertToNodeSpaceAR(worldPoint);
        this.shuziNode.x = localPoint.x;
        this.shuziNode.y = localPoint.y;
        let box = this.pengNode.getBoundingBox();
        let boneBox = this.shuziNode.getBoundingBox();
        if(boneBox.intersects(box)){
            this.shuziNode.x = this._selectPos1.x;
            this.shuziNode.y = this._selectPos1.y;
            this.wrongNode.active = true;
            //@ts-ignore
            this.scheduleOnce(()=>{
                this.wrongNode.active = false;
            },1);
        }
    };
    onTouchEnd(event){
        if(this._done)
            return;
        this.shuziNode.x = this._selectPos1.x;
        this.shuziNode.y = this._selectPos1.y;
    };
    onTouchMove1(event){
        if(this._done)
            return;
        let worldPoint = event.touch.getLocation();
        let localPoint = this.kouhongNode.parent.convertToNodeSpaceAR(worldPoint);
        this.kouhongNode.x = localPoint.x;
        this.kouhongNode.y = localPoint.y;
        let box = this.pengNode.getBoundingBox();
        let boneBox = this.kouhongNode.getBoundingBox();
        if(boneBox.intersects(box)){
            this._done = true;
            this.kouhongNode.active = false;
            Utils.audioManager.playSound("huazhuang/deputyGame/step_10");
            this.spine.setAnimation(0,"correct",false);
            this.spine.setCompleteListener(()=>{
                this.endNode.active = true;
                Utils.audioManager.playSound("huazhuang/deputyGame/pass");
            });
        }
    };
    onTouchEnd1(event){
        if(this._done)
            return;
        this.kouhongNode.x = this._selectPos2.x;
        this.kouhongNode.y = this._selectPos2.y;
    };
    
    onClickEnd(isSkip){
        let next = this.params.next && this.params.next.shift();
        if (next) {
            UIHelper.openUI(next, {
                next: this.params.next
            })
            this.node.destroy();
        }
    };
    
    onClickSkip(){
        this.onClickEnd(true);
    };

    //提示
    onClickTips(){
        PopUIHelper.helper.showNewConfirmMsg(i18n.t("DEPUTY_GAME_TIPS10"));
    };

    
    
}
